<?php

namespace Admin\Controller;

/**
 * 后台默认控制器类
 * Class IndexController
 * @package Admin\Controller
 */
class IndexController extends CommonController
{
    /**
     * 取出当前登陆管理帐号所拥有的权限菜单列表
     * 显示管理后台框架
     */
    public function index()
    {
        $user_id = session('user_id');
        $admin_id = session('admin_id');
        $admin = M('admin')->where(['admin_id'=>$admin_id])->find();
        //if(empty($admin)){
            $user = M('user')->where(['user_id'=>$user_id])->find();
        //}
        $this->assign('admin',$admin);
        $this->assign('user',$user);
     //   echo 'ok';
     
       // $user_id= session('user_id');
         // $admin_id = 1;
       /*
      if( $admin_id<2)
      {
          $admin_id=1;
      }
      else{

              $admin_id=7;


      }*/

        //如果是用户取用户ID,查询该用户的角色
      //  $user_id=session('user_id');
/*
        $user_arr_1 = M('User')->where(['level_id' => 1])->getField('user_id', true);
        $user_1 = implode(',', $user_arr_1);
        $where1['push_user_id'] = ['in', $user_1];
        $level1 = $this->tongji($where1);

        $user_arr_2 = M('User')->where(['level_id' => 2])->getField('user_id', true);
        $user_2 = implode(',', $user_arr_2);
        $where2['push_user_id'] = ['in', $user_2];
        $level2 = $this->tongji($where2);

        $user_arr_3 = M('User')->where(['level_id' => 3])->getField('user_id', true);
        $user_3 = implode(',', $user_arr_3);
        $where3['push_user_id'] = ['in', $user_3];
        $level3 = $this->tongji($where3);

        $user_arr_4 = M('User')->where(['level_id' => 4])->getField('user_id', true);
        $user_4 = implode(',', $user_arr_4);
        $where4['push_user_id'] = ['in', $user_4];
        $level4 = $this->tongji($where4);

        $user_arr_5 = M('User')->where(['level_id' => 5])->getField('user_id', true);
        $user_5 = implode(',', $user_arr_5);
        $where5['push_user_id'] = ['in', $user_5];
        $level5 = $this->tongji($where5);

*/


        // 根据管理员编号查询角色编号
    //   $role_id = M('AdminRole')->where(['admin_id'=>$admin_id])->getField('role_id');
        // 角色编号（1：超级管理员，2：公司，3：部门，4：车间，5：班组）
  //   $pid = $role_id-1;
     //  $list = M('User')->where(['parent_id'=>0, 'status'=>1])->select();

        $menu = D('Admin/Privilege')->getMenu( $admin_id );
        $this->assign('menu', $menu);
//       $this->assign('level1', $level1);
//       $this->assign('level2', $level2);
//       $this->assign('level3', $level3);
//         $this->assign('level4', $level4);
//        $this->assign('level5', $level5);
     //   $this->assign('list', $list);


        if (IS_GET) {

            $fxd = $_GET['fxd'];
            //根据风险点名称查询出风险点ID
            if ($fxd != '') {

                $danger_id = M('Danger')->where(['name' => $fxd])->getField('id');
                $where1['danger_id'] = $danger_id;
            }
            // echo $danger_id;
        }
        $where1['status']='1';
        $Data = M('Object'); // 实例化Data数据模型
        $count   = $Data->where( $where1)->count();
        $list = $this->lists('Object',$where1, 'object_id asc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
                $list[$k]['object_level_name'] = M('ObjectLevel')->where(['id' => $v['object_level_id']])->getField('name');

// 根据危险源ID object_id 查询该危险源本月，累计巡检次数XX次，未巡检多少次
                //execute返回结果数
                $start = date('Y-m-01 00:00:00');
                $end = date('Y-m-d H:i:s');
                $sqlw= "SELECT push_id  FROM `xc_push_record` WHERE object_id =".$v['object_id']." and `create_time` >= unix_timestamp('”.$start.”') AND `create_time` <= unix_timestamp('$end')";
                $model = M( "push_record" );
                // 本月已经巡检次数count1,
                $list[$k]['count1']  =$model ->execute($sqlw);

                //  本月岗位级别级巡检次数count_gs1 ， 未巡检次数等于当天日期减去已巡检次数  date('d') -
                $sqlgw=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=5  and t.status=1)";
                $gw1 = M( "push_record" );
                // 本月员工巡检次数yg1，未巡检次数yg2,
                $list[$k]['yg1']  =$gw1 ->execute($sqlgw);
                $list[$k]['yg2']  =   date('d') - $list[$k]['yg1']  ;
                //本月班组级别级巡检次数count_gs1 ， 未巡检次数等于当天日期减去已巡检次数  date('d') -
                $sqlbz=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=4  and t.status=1)";
                $bz1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['bz1']  =$bz1 ->execute($sqlbz);
                $list[$k]['bz2']  =   date('d') - $list[$k]['bz1']  ;
                //本月车间级别级巡检次数cj1 ， 未巡检次数 风险点车间级每月巡检4次-
                $sqlcj=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=3  and t.status=1)";
                $cj1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['cj1']  =$cj1 ->execute($sqlcj);
                $list[$k]['cj2']  = 4 - $list[$k]['cj1']  ;
                //本月专业级别级巡检次数cj1 ， 未巡检次数 风险点车间级每月巡检4次-
                $sqlzy=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=2  and t.status=1)";
                $zy1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['zy1']  =$zy1 ->execute($sqlzy);
                $list[$k]['zy2']  = 3 - $list[$k]['zy1']  ;
                //  本月公司级别级巡检次数count_gs1 ，公司级应每月巡检1次
                $sqlgs=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=1  and t.status=1)";
                $gs1 = M( "push_record" );
                // 本月公司巡检次数count1,
                $list[$k]['gs1']  =$gs1 ->execute($sqlgs);
                $list[$k]['gs2']  = 1 - $list[$k]['gs1']  ;

                $list[$k]['count2']  =   $list[$k]['gs2']+ $list[$k]['zy2'] +    $list[$k]['cj2']+ $list[$k]['yg2']+  $list[$k]['bz2'];

            }
        }




        $this->assign('list', $list);
        $Page     = new \Think\Page($count, 20);
        $show      = $Page->show();
        $this->assign('page', $show);
        $this->assign('admin_id',$admin_id);
        $this->display();







      //  $this->redirect('Admin/Search/search_list');
   //  $this->display();
    }
    public function welcome(){
        $admin_id = session('admin_id');
        $menu = D('Admin/Privilege')->getMenu( $admin_id );
        $this->assign('menu', $menu);

        if (IS_GET) {

            $fxd = $_GET['fxd'];
            //根据风险点名称查询出风险点ID
            if ($fxd != '') {

                $danger_id = M('Danger')->where(['name' => $fxd])->getField('id');
                $where1['danger_id'] = $danger_id;
            }
            // echo $danger_id;
        }
        $where1['status']='1';
        $Data = M('Object'); // 实例化Data数据模型
        $count   = $Data->where( $where1)->count();
        $list = $this->lists('Object',$where1, 'object_id asc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
                $list[$k]['object_level_name'] = M('ObjectLevel')->where(['id' => $v['object_level_id']])->getField('name');

                // 根据危险源ID object_id 查询该危险源本月，累计巡检次数XX次，未巡检多少次
                //execute返回结果数
                $start = date('Y-m-01 00:00:00');
                $end = date('Y-m-d H:i:s');
                $sqlw= "SELECT push_id  FROM `xc_push_record` WHERE object_id =".$v['object_id']." and `create_time` >= unix_timestamp('”.$start.”') AND `create_time` <= unix_timestamp('$end')";
                $model = M( "push_record" );
                // 本月已经巡检次数count1,
                $list[$k]['count1']  =$model ->execute($sqlw);

                //  本月岗位级别级巡检次数count_gs1 ， 未巡检次数等于当天日期减去已巡检次数  date('d') -
                $sqlgw=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=5  and t.status=1)";
                $gw1 = M( "push_record" );
                // 本月员工巡检次数yg1，未巡检次数yg2,
                $list[$k]['yg1']  =$gw1 ->execute($sqlgw);
                $list[$k]['yg2']  =   date('d') - $list[$k]['yg1']  ;
                //本月班组级别级巡检次数count_gs1 ， 未巡检次数等于当天日期减去已巡检次数  date('d') -
                $sqlbz=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=4  and t.status=1)";
                $bz1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['bz1']  =$bz1 ->execute($sqlbz);
                $list[$k]['bz2']  =   date('d') - $list[$k]['bz1']  ;
                //本月车间级别级巡检次数cj1 ， 未巡检次数 风险点车间级每月巡检4次-
                $sqlcj=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=3  and t.status=1)";
                $cj1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['cj1']  =$cj1 ->execute($sqlcj);
                $list[$k]['cj2']  = 4 - $list[$k]['cj1']  ;
                //本月专业级别级巡检次数cj1 ， 未巡检次数 风险点车间级每月巡检4次-
                $sqlzy=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=2  and t.status=1)";
                $zy1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['zy1']  =$zy1 ->execute($sqlzy);
                $list[$k]['zy2']  = 3 - $list[$k]['zy1']  ;
                //  本月公司级别级巡检次数count_gs1 ，公司级应每月巡检1次
                $sqlgs=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=1  and t.status=1)";
                $gs1 = M( "push_record" );
                // 本月公司巡检次数count1,
                $list[$k]['gs1']  =$gs1 ->execute($sqlgs);
                $list[$k]['gs2']  = 1 - $list[$k]['gs1']  ;

                $list[$k]['count2']  =   $list[$k]['gs2']+ $list[$k]['zy2'] +    $list[$k]['cj2']+ $list[$k]['yg2']+  $list[$k]['bz2'];

            }
        }




        $this->assign('list', $list);
        $Page     = new \Think\Page($count, 20);
        $show      = $Page->show();
        $this->assign('page', $show);

        $this->display();

    }
    /**
     * AJAX获取下级列表
     */

    public function get_next_level()
    {
        $user_id = I('get.user_id');
        if (empty($user_id)) {
            echo '';die;
        }
        $user = M('User')->where(['user_id'=>$user_id, 'status'=>1])->find();
        if ($user['level_id'] == 5) {
            $object_ids = explode(',', $user['object_ids']);
            foreach ($object_ids as $k => $object_id) {
                $object_ids[$k] = M('Object')->where(['object_id'=>$object_id])->find();
            }
            foreach ($object_ids as $k => $v) {
                $object_ids[$k]['object_type'] = M('ObjectType')->where(['id'=>$v['type_id']])->getField('name');
                $object_ids[$k]['danger_name'] = M('Danger')->where(['id'=>$v['danger_id']])->getField('name');
            }
            $this->assign('list', $object_ids);
            echo $this->fetch('get_objects');die;
        } else {
            $list = M('User')->where(['parent_id'=>$user_id, 'status'=>1])->select();
            $this->assign('list', $list);
            echo $this->fetch('get_next_level');
        }
    }

    public function test()
    {
        /*$get = I('get.');
        if (isset($get['status'])) {
            $where['status'] = $get['status'];
        }
        if (isset($get['is_danger'])) {
            $where['is_danger'] = $get['is_danger'];
        }*/
        $user_arr_1 = M('User')->where(['level_id' => 1])->getField('user_id', true);
        $user_1 = implode(',', $user_arr_1);
        $where['push_user_id'] = ['in', $user_1];
        $level1 = $this->tongji($where);
        $user_arr_2 = M('User')->where(['level_id' => 2])->getField('user_id', true);
        $user_2 = implode(',', $user_arr_2);
        $where['push_user_id'] = ['in', $user_2];
        $level2 = $this->tongji($where);
        $user_arr_3 = M('User')->where(['level_id' => 3])->getField('user_id', true);
        $user_3 = implode(',', $user_arr_3);
        $where['push_user_id'] = ['in', $user_3];
        $level3 = $this->tongji($where);
        $user_arr_4 = M('User')->where(['level_id' => 4])->getField('user_id', true);
        print_r($user_arr_4);die;
        $user_4 = implode(',', $user_arr_4);
        $where['push_user_id'] = ['in', $user_4];
        $level4 = $this->tongji($where);
        echo $level4;die;
    }

    public function tongji($where)
    {
        // 全部风险点
        $all_danger = M('Object')->where($where)->where(['status' => 1])->count();
        // 本月共排查
        $all = M('PushRecord')->where($where)->count();
        // 风险
        $danger = M('PushRecord')->where($where)->where(['is_danger' => 1])->count();
        // 已解决风险
        $is_deal = M('PushRecord')->where($where)->where(['is_danger' => 1, 'status' => 2])->count();
        // 未解决
        $where['status'] = ['elt', 1];
        $where['is_danger'] = 1;
        $to_deal = M('PushRecord')->where($where)->where($where)->count();
        $level = $all_danger.','.$all.','.$danger.','.$is_deal.','.$to_deal;
        return $level;
    }
}
